import 'package:enterprise_v6_app/app/components/custom_modele/new_my_columnar_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/training_result_department_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/train_program/online_training/train_result/train_result_controller.dart';

///培训结果
class TrainResultPage extends StatelessWidget {
  TrainResultPage({super.key});

  final controller = Get.put(TrainResultController());

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        margin: EdgeInsets.only(left: 16.w, top: 12.w, right: 16.w, bottom: 12.w),
        padding: EdgeInsets.only(left: 12.w, top: 14.w, right: 12.w, bottom: 14.w),
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        child: Column(
          children: [
            AppGaps.vGap12,
            Container(
              padding: EdgeInsets.symmetric(vertical: 10.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: const Color(0x1A5C6DFF),
              ),
              child: Row(
                children: [
                  Expanded(
                    child: Column(
                      children: [
                        _topTitle('计划数', const Color(0xFF5C6DFF), '${controller.peopleCountingEntity?.planNum ?? 0}', const Color(0xFF5C6DFF)),
                        AppGaps.vGap10,
                        _topTitle('缺席', const Color(0xFF5C6DFF), '${controller.peopleCountingEntity?.numberOfAbsentees ?? '0'}', const Color(0xFFFC9900)),
                      ],
                    ),
                  ),
                  _divider(),
                  Expanded(
                    child: Column(
                      children: [
                        _topTitle('未达标', const Color(0xFF5C6DFF), '${controller.peopleCountingEntity?.notMeetingStandards ?? '0'}', const Color(0xFFFD6B6B)),
                        AppGaps.vGap10,
                        _topTitle('补考', const Color(0xFF5C6DFF), '${controller.peopleCountingEntity?.makeUpExamination ?? '0'}', const Color(0xFF33A9FF)),
                      ],
                    ),
                  ),
                  _divider(),
                  Expanded(
                      child: Column(
                    children: [
                      _topTitle('达标数', const Color(0xFF5C6DFF), '${controller.peopleCountingEntity?.compliance ?? '0'}', const Color(0xFF5C6DFF)),
                      AppGaps.vGap10,
                      _topTitle('', const Color(0xFF5C6DFF), '', const Color(0xFF5C6DFF)),
                    ],
                  )),
                ],
              ),
            ),
            AppGaps.vGap20,
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _riskTypeWidget('完成课时人数', const Color(0xFF5C6DFF)),
                AppGaps.hGap20,
                _riskTypeWidget('未完成课时人数', const Color(0xffFD6B6B)),
              ],
            ),
            NewColumnarBar.columnarBar(
              width: 5.w,
              height: 150.w,
              xAxisList: controller.hiddenDangerAxisList,
              yAxis: controller.hiddenDangerYAxis,
              onCallBack: (index) {
                // 掉接口 线上培训结果 部门列表 默认获取第一行的
                controller.trainingResultDepartmentList(textBookId: controller.courseStatisticsEntity?[index].textbookId ?? '');
              },
            ),
            _geBottomView(),
          ],
        ),
      ),
    );
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Row(
      children: [
        AppGaps.hGap16,
        Text(
          title,
          style: TextStyle(
            fontSize: 12.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap8,
        Expanded(
          child: Text(
            textAlign: TextAlign.end,
            value ?? '空',
            style: TextStyle(
              fontSize: 16.sp,
              color: square,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
        AppGaps.hGap16,
      ],
    );
  }

  // 类型显示
  Widget _riskTypeWidget(String name, Color color) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          width: 8.w,
          height: 8.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(40.w)),
            color: color,
          ),
        ),
        AppGaps.hGap10,
        Text(
          name,
          style: TextStyle(color: const Color(0xff333333), fontSize: 12.sp),
        ),
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 24.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ])),
    );
  }

  Widget _geBottomView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(vertical: 10.w),
            decoration: BoxDecoration(
              color: const Color(0xFFF1F2F7),
              borderRadius: BorderRadius.all(
                Radius.circular(6.w),
              ),
            ),
            child: Row(
              children: [
                Expanded(
                    flex: 1, child: Center(child: Text('姓名', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 12.sp, color: const Color(0xFF495061))))),
                Expanded(
                    flex: 1, child: Center(child: Text('部门', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 12.sp, color: const Color(0xFF495061))))),
                Expanded(
                    flex: 1, child: Center(child: Text('分数', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 12.sp, color: const Color(0xFF495061))))),
                Expanded(
                    flex: 1, child: Center(child: Text('课时', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 12.sp, color: const Color(0xFF495061))))),
                Expanded(
                    flex: 1,
                    child: Center(child: Text('考核结果', style: TextStyle(fontWeight: FontWeight.w500, fontSize: 12.sp, color: const Color(0xFF495061))))),
              ],
            ),
          ),
          ListView.separated(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(vertical: 10.w),
                  decoration: BoxDecoration(
                    color: index % 2 != 0 ? const Color(0xFFF1F2F7) : Colors.white,
                    borderRadius: BorderRadius.all(
                      Radius.circular(6.w),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      _rowFirst(controller.trainingResultDepartmentListEntity?[index].userName ?? ''),
                      _rowFirst(controller.trainingResultDepartmentListEntity?[index].userDepartmentName ?? ''),
                      _rowFirst('${controller.trainingResultDepartmentListEntity?[index].score ?? ''}'),
                      _rowFirst(
                          '${controller.trainingResultDepartmentListEntity?[index].finishHours ?? ''}(${controller.trainingResultDepartmentListEntity?[index].classHour ?? ''})'),
                      _rowFirst(controller.trainingResultDepartmentListEntity?[index].score==0?'未考核':_assessmentResults(controller.trainingResultDepartmentListEntity?[index])),
                    ],
                  ),
                );
              },
              separatorBuilder: (context, index) => const SizedBox(width: 0, height: 0),
              itemCount: controller.trainingResultDepartmentListEntity?.length ?? 0)
        ],
      ),
    );
  }

  Widget _rowFirst(String name) {
    return Expanded(
      flex: 1,
      child: Center(
        child: Text(
          name,
          style: TextStyle(
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
            color: const Color(0xFF818499),
          ),
        ),
      ),
    );
  }

  ///考核结果
  String _assessmentResults(TrainingResultDepartmentListEntity? trainingResultDepartmentListEntity) {
    String result = '';
    //我的得分
    final int score = trainingResultDepartmentListEntity?.score ?? 0;
    //总分
    final int totalScore = trainingResultDepartmentListEntity?.totalScore ?? 0;
    if (totalScore != 0 && score < totalScore * 0.7 && score >= totalScore * 0.6) {
      //我的分数大于等于60% 小于70
      result = '合格';
    } else if (totalScore != 0 && score < totalScore * 0.9 && score >= totalScore * 0.7) {
      //我的分数大于等于70%  小于90%
      result = '良好';
    } else if (totalScore != 0 && score <= totalScore * 1 && score >= totalScore * 0.9) {
      //我的分数大于等于90%  小于等于100%
      result = '优秀';
    } else if (totalScore != 0 && score < totalScore * 0.6) {
      //我的分数小于60%
      result = '不合格';
    }
    return result;
  }
}
